/**
 * 
 */
package io.gitee.falllee.dao.core.table;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import io.gitee.falllee.dao.config.DaoConfigContextHolder;
import io.gitee.falllee.dao.model.BaseDaoConfig;
import io.gitee.falllee.dao.util.DaoUtils;
import io.gitee.falllee.dao.util.FilterPlus;
import io.gitee.falllee.dao.util.SortPlus;

/**
 * @author batty
 * @date 2022-06-28
 *
 */
public class SelectMultiFieldBuilder {

    private SelectBuilder selectBuilder;
    
    private BaseDaoConfig daoConfig;
    
    private String sort;
    
    private FilterPlus filter;
    
    private List<String> params;
    
    SelectMultiFieldBuilder(SelectBuilder parent, BaseDaoConfig daoConfig) {
        selectBuilder = parent;
        this.daoConfig = daoConfig;
    }
    
    public SelectMultiFieldBuilder filter(FilterPlus filterPlus) {
        this.filter = filterPlus;
        return this;
    }
    
    public SelectMultiFieldBuilder sort(SortPlus sortPlus) {
        this.sort = sortPlus.getSort();
        return this;
    }
    
    public SelectMultiFieldBuilder sort(String sort) {
        this.sort = sort;
        return this;
    }
    
    public SelectMultiFieldBuilder addChildrenTable(String tableName) {
        if (params == null) {
            params = new ArrayList<>();
        }
        params.add(tableName);
        return this;
    }
    
    public List<Map<String, Object>> select(List<LinkedHashMap<String, Object>> list) {
        String[] pars = DaoUtils.list2Array(params);
        if (filter == null) {
            filter = FilterPlus.build();
        }
        if (DaoConfigContextHolder.getAddGlobalFilter()) {
            filter = daoConfig.addGlobalFilter(filter);
        }
        return selectBuilder.selectByMultiFieldList(list, filter, sort, pars);
    }
}
